package com.bloveoacom.weichi.activity;

import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.text.Layout;
import android.text.SpannableStringBuilder;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.SpanUtils;
import com.bloveoacom.base.BaseList2Activity;
import com.bloveoacom.base.UserInfoBean;
import com.bloveoacom.http.HttpMethods;
import com.bloveoacom.http.RxHelper;
import com.bloveoacom.observer.BaseSubscriber2;
import com.bloveoacom.util.ActivityUtil;
import com.bloveoacom.util.Constant;
import com.bloveoacom.util.DataUtil;
import com.bloveoacom.util.GlideUtil;
import com.bloveoacom.util.GsonUtil;
import com.bloveoacom.util.NumberUtil;
import com.bloveoacom.util.StringUtils;
import com.bloveoacom.weichi.R;
import com.bloveoacom.weichi.bean.NewsPostBean;
import com.bloveoacom.weichi.bean.UserHomeInfo;
import com.bloveoacom.weight.LlkjToolBar;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.bloveoacom.http.HttpMethods.PAGE_SIZE;


/**
 * author: zhu.px
 * e-mail: 1271901673@qq.com
 * time  : 2018/5/28
 * desc  : 关注公众号
 */
public class UserInfo2Activity extends BaseList2Activity<NewsPostBean> {
    @BindView(R.id.title_bar)
    LlkjToolBar mToolbar;
    private String userid;

    @Override
    public int getLayoutResId() {
        return R.layout.layout_refresh_title;
    }

    @Override
    public void initView() {
        super.initView();
        mToolbar.setToolBar("个人主页",
                true, "", R.drawable.ic_back, true, "", LlkjToolBar.isNull);
    }

    @Override
    public void getBundleExtras(Bundle extras) {
        userid = extras.getString(Constant.DATA0);
    }

    @Override
    protected View getViewSetTB() {
        return mToolbar;
    }

    @Override
    protected void convertView(BaseViewHolder helper, NewsPostBean item) {
        DataUtil.setItemView2(mContext,helper,item,0);
        helper.setVisible(R.id.group2,false);
        helper.setVisible(R.id.group,true);
    }

    @Override
    protected int getItemLayout() {
        return R.layout.rv_item_post2;
    }

    @Override
    protected void getData() {
        HttpMethods.getInstance().getHttpService().queryHomePageInfo(UserInfoBean.getInstance().getToken(), userid, page, PAGE_SIZE).compose(RxHelper.getData()).subscribeWith(new BaseSubscriber2<Object>(this) {
            @Override
            public void onNext(Object o) {
                super.onNext(o);
                String s = GsonUtil.GsonString(o);
                userHomeInfo = GsonUtil.GsonToBean(s, UserHomeInfo.class);
                GlideUtil.loadCircleImage(holder.ivHeader, StringUtils.getFullUrl(userHomeInfo.headImg, 0));
                holder.tvUsername.setText(userHomeInfo.name);
                holder.tvFansNum.setText(getSpan(userHomeInfo.fansNum,"粉丝 "));
                holder.tvAttentionNum.setText(getSpan(userHomeInfo.focusNum,"关注 "));
                doData(userHomeInfo.coinBarNewsList);
            }
        });
    }
    public SpannableStringBuilder getSpan(String fData, String sData){
        return new SpanUtils().append(sData).setAlign(Layout.Alignment.ALIGN_CENTER).setForegroundColor(ContextCompat.getColor(this, R.color.gray_text_color)).append(fData).setForegroundColor(ContextCompat.getColor(mContext, R.color.black)).create();
    }
    @Override
    public void addViewForAdapter() {
        adapter.addHeaderView(getHeaderView());
    }

    private ViewHolder holder;

    private View getHeaderView() {
        View view = LayoutInflater.from(mContext).inflate(R.layout.header_user_info, null);
        holder = new ViewHolder(view);
        return view;

    }


    @Override
    public void initListener() {
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Bundle bundle = new Bundle();
                bundle.putInt(PostDetailActivity.COINID, NumberUtil.getIntByStr(datas.get(position).coinId));
                ActivityUtil.startActivity(mContext, PostDetailActivity.class, bundle);
            }
        });
    }

    UserHomeInfo userHomeInfo;


    static class ViewHolder {
        @BindView(R.id.iv_header)
        ImageView ivHeader;
        @BindView(R.id.tv_username)
        TextView tvUsername;
        @BindView(R.id.tv_fans_num)
        TextView tvFansNum;
        @BindView(R.id.tv_attention_num)
        TextView tvAttentionNum;

        @OnClick({R.id.tv_fans_num,R.id.tv_attention_num})
        public void click(View view){

        }
        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
